package org.jivesoftware.spark.crowd;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jivesoftware.Spark;

public class CrowdRecorder {
	private static SimpleDateFormat sdf ;
	private static File file ;
	private static final File path = Spark.getLogDirectory();
	
	static{
		sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SS");
		
		file = new File(path,"crowdLog.txt");
//		file.delete();
		try {
			if(!file.exists())
				file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void record(int message){
		record(((Integer)message).toString());
	}
	public static void record(String message){
		FileOutputStream fos = null;
		OutputStreamWriter out = null;
		PrintWriter pr = null;
		try {
			fos = new FileOutputStream(file,true);
			out = new OutputStreamWriter(fos,"utf-8");
			pr = new PrintWriter(out);
			Date now = new Date();
			String now_string = sdf.format(now);
			pr.println("\n"+message+"\t\t"+now_string);
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally{
			try{
				if(pr!=null)
			pr.close();
				if(out!=null)
			out.close();
				if(fos!=null)
			fos.close();
				
			}catch(Exception e){}
		}
	}
	public static void recordOnConsol(String message){
		Date now = new Date();
		String now_string = sdf.format(now);
		System.out.println("\n"+message+"\t\t"+now_string);
	}
}
